<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;

use Think\Controller;

header('content-type: text/html;charset=utf8');

class RedisController extends Controller
{
	/**
	 * 手动启动redis轮询
	 * */
	public function runRedis()
	{
		vendor('redis.DelayQueue');
		$dq = new \DelayQueue();

		$task = $dq->getTask();
		$task = $task[0];

		if ($task) {
			//有并发的可能，这里通过zrem返回值判断谁抢到该任务
			if ($dq->delTask($task)) {
				$task = json_decode($task, true);

				file_put_contents(RUNTIME_PATH . 'task.txt', '执行时间：' . date('Y-m-d H:i:s') . '；' . json_encode($task) . PHP_EOL, FILE_APPEND);

				//业务逻辑处理
				if ($task['task_name'] == 'groupStart' || $task['task_name'] == 'groupEnd') {
					//拼团已到开始时间或者结束时间
					groupCtr($task['task_params']['group_work_id'], $task['task_params']['category'], $task['task_params']['course_id'], $task['task_name'] == 'groupStart' ? 1 : 2);
				} else if ($task['task_name'] == 'skillStart' || $task['task_name'] == 'skillEnd') {
					//秒杀已到开始时间或者结束时间
					skillCtr($task['task_params']['seckill_id'], $task['task_params']['category'], $task['task_params']['course_id'], $task['task_name'] == 'skillStart' ? 1 : 2);
				} else if ($task['task_name'] == 'groupChiefEnd') {
					//用户发起的单个拼团已到结束时间
					groupFail($task['task_params']['group_join_id']);
				}
			}
		}
	}

	/**
	 * 手动启动redis轮询
	 * */
	public function runTestRedis()
	{
		$task = '{"task_name":"groupChiefEnd","task_time":1617180555,"task_params":{"group_join_id":1111}}';
		$task = json_decode($task, true);

		//业务逻辑处理
		if ($task['task_name'] == 'groupStart' || $task['task_name'] == 'groupEnd') {
			//拼团已到开始时间或者结束时间
			groupCtr($task['task_params']['group_work_id'], $task['task_params']['category'], $task['task_params']['course_id'], $task['task_name'] == 'groupStart' ? 1 : 2);
		} else if ($task['task_name'] == 'skillStart' || $task['task_name'] == 'skillEnd') {
			//秒杀已到开始时间或者结束时间
			skillCtr($task['task_params']['seckill_id'], $task['task_params']['category'], $task['task_params']['course_id'], $task['task_name'] == 'skillStart' ? 1 : 2);
		} else if ($task['task_name'] == 'groupChiefEnd') {
			//用户发起的单个拼团已到结束时间
			groupFail($task['task_params']['group_join_id']);
		}
	}

	/**
	 * 添加任务
	 * @param        string        $task_name         任务名
	 * @param        int           $task_time         执行时间戳
	 * @param        array         $task_param        携带参数
	 * */
	public function addRedis($task_name, $task_time, $task_param)
	{
		vendor('redis.DelayQueue');
		$dq = new \DelayQueue();

		$dq->addTask($task_name, $task_time, $task_param);
	}

	public function getRedis()
	{
		vendor('redis.DelayQueue');
		$dq = new \DelayQueue();
		$list = $dq->getTaskTest();
		echo "<pre>";
		print_r($list);
		exit;
	}

	public function delRedis()
	{
		vendor('redis.DelayQueue');
		$dq = new \DelayQueue();

		$task = $dq->getTask();
		$task = $task[0];

		$res = $dq->delTask($task);
		print_r($res);
	}
}
